<template>
  <div class="WorkingSchedule" v-loading="loading">
    <el-form ref="form" :model="form" label-position="top" class="myform">
      <div>
        <el-form-item label="款号" class="myform-item">
          <el-input
            v-model="form.type_no"
            placeholder="请输入款号"
            @keyup.enter.native="onSearch"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="交货日期">
          <el-date-picker
            value-format="yyyy-MM-dd"
            v-model="date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
      </div>
      <div>
        <el-form-item style="margin-top: 50px">
          <el-button @click="onSearch">查询</el-button>
          <el-button type="primary" @click="postExcelamoountexport_new" :loading="downloadloading" :disabled="!qxobj.export">导出</el-button
          >
          <!-- <el-button type="primary">上传文件</el-button> -->
        </el-form-item>
      </div>
    </el-form>
    <el-table :data="tableData" style="width: 100%" border>
      <template v-for="(item, index) in tableHeader">
        <el-table-column
          v-if="item.key != 'notes' && item.key != 'y_date'"
          :label="item.name"
          :key="index"
          align="center"
          :prop="item.key"
        >
        </el-table-column>
        <el-table-column
          v-if="item.key == 'notes'"
          :label="item.name"
          :key="index"
          align="center"
          :prop="item.key"
        >
          <template slot-scope="{ row }">
            <div v-for="(items, idx) in row.notes" :key="idx">
              {{ items.ad_name }}:{{ items.notes }}({{ items.createtime }})
            </div>
          </template>
        </el-table-column>
        <el-table-column
          v-if="item.key == 'y_date'"
          :label="item.name"
          :key="index"
          align="center"
          :prop="item.key"
        >
          <template slot-scope="{ row }">
            <div v-for="(items, idx) in row.y_date" :key="idx">
              {{ items.y_date }}
            </div>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="postApiamount"
    />
  </div>
</template>

<script>
import { postApiamount, postExcelamoountexport_new } from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      qxobj:{},
      loading: false,
      downloadloading:false,
      date: "",
      form: {
        type_no: "",
        strtime: "",
        endtime: "",
        page: 1,
        size: 10,
      },
      tableHeader: [
        {
          name: "交货日期",
          key: "f_date",
        },
        {
          name: "商品编码",
          key: "product_code",
        },
        {
          name: "款号",
          key: "type_no",
        },
        {
          name: "商品名称",
          key: "product_name",
        },
        {
          name: "延期说明",
          key: "notes",
        },
        {
          name: "下次交货日期",
          key: "y_date",
        },
        {
          name: "数量",
          key: "sum",
        },
      ],
      tableData: [],
      total: 1,
    };
  },
  activated() {
    this.qxobj = JSON.parse(this.$route.query.qxobj)
    let form = this.form;
    this.postApiamount(form);
  },
  methods: {
    // 导出
    postExcelamoountexport_new() {
      this.downloadloading = true
      let obj = {}
      let date = this.date;
      if (date) {
        obj.time = date[0]+'/'+date[1]
      }
      postExcelamoountexport_new(obj).then(res=>{
        this.downloadloading = false
        window.location.href = res.url
      }).catch(err=>{
        this.downloadloading = false
      })
    },
    // 编辑
    onEdit(e) {
      console.log("编辑", e);
      this.detail = e;
      this.dialogVisible = true;
    },
    // 采购列表
    async postApiamount(data) {
      this.loading = true;
      this.form = data;
      postApiamount(data)
        .then((res) => {
          this.loading = false;
          console.log("采购列表", res);
          this.tableData = res.data;
          this.total = Number(res.total);
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 搜索
    onSearch() {
      let date = this.date;
      if (date) {
        this.form.strtime = date[0];
        this.form.endtime = date[1];
      }
      this.form.page = 1;
      this.form.size = 10;
      this.postApiamount(this.form);
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.WorkingSchedule {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
}
</style>
